{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "452bcb1d",
   "metadata": {},
   "source": [
    "使用os模块操作文件以及目录路径，定义一个统计指定**文件夹**大小的函数，（返回文件夹大小【非占用空间】）单位：字节(B)\n",
    "提示：os.path.getsize(file) #获取文件大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0ea88508",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import os.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6a262688",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4096"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.path.getsize('../代码')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9d6689aa",
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19,358 bytes in 26 files\n"
     ]
    }
   ],
   "source": [
    "def getSize(path): \n",
    "    totalsize,filecnt = 0,0 \n",
    "    for root, dirs, files in os.walk(path): \n",
    "       for file in files: \n",
    "         tgt=os.path.join(root,file) \n",
    "         if os.path.exists(tgt): \n",
    "            size = os.stat(tgt).st_size \n",
    "            totalsize = totalsize + size \n",
    "            filecnt+=1 \n",
    "    return totalsize,filecnt \n",
    "\n",
    "print('{:,} bytes in {:,} files'.format(*getSize('../代码'))) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4da29ff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "679ded2e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
